Clover icon

compiler

  1. Project Clover database Mon Jan 2 2023 15:09:37 MST
  2. Package com.google.javascript.jscomp

File JSError.java

 

Coverage histogram

../../../../img/srcFileCovDistChart6.png
87% of files have more coverage

Code metrics

38
54
18
1
291
134
41
0.76
3
18
2.28

Classes

Class Line # Actions
JSError 26 54 41 48
0.5636363656.4%
 

Contributing tests

This file is covered by 4908 tests. .

Source view

1    /*
2    * Copyright 2004 The Closure Compiler Authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16    package com.google.javascript.jscomp;
17   
18    import com.google.javascript.rhino.Node;
19   
20    import javax.annotation.Nullable;
21   
22    /**
23    * Compile error description
24    *
25    */
 
26    public class JSError {
27    /** A type of the error */
28    private final DiagnosticType type;
29   
30    /** Description of the error */
31    public final String description;
32   
33    /** Name of the source */
34    public final String sourceName;
35   
36    /** Node where the warning occurred. */
37    final Node node;
38   
39    /** Line number of the source */
40    public final int lineNumber;
41   
42    /** @deprecated Use #getDefaultLevel */
43    @Deprecated
44    public final CheckLevel level;
45   
46    private final CheckLevel defaultLevel;
47   
48    // character number
49    private final int charno;
50   
51    //
52    // JSError.make - static factory methods for creating JSError objects
53    //
54    // The general form of the arguments is
55    //
56    // [source location] [level] DiagnosticType [argument ...]
57    //
58    // This order echos a typical command line diagnostic. Source location
59    // arguments are arranged to be sources of information in the order
60    // file-line-column.
61    //
62    // If the level is not given, it is taken from the level of the
63    // DiagnosticType.
64   
65   
66    /**
67    * Creates a JSError with no source information
68    *
69    * @param type The DiagnosticType
70    * @param arguments Arguments to be incorporated into the message
71    */
 
72  42 toggle public static JSError make(DiagnosticType type, String... arguments) {
73  42 return new JSError(null, null, -1, -1, type, null, arguments);
74    }
75   
76    /**
77    * Creates a JSError at a given source location
78    *
79    * @param sourceName The source file name
80    * @param lineno Line number with source file, or -1 if unknown
81    * @param charno Column number within line, or -1 for whole line.
82    * @param type The DiagnosticType
83    * @param arguments Arguments to be incorporated into the message
84    */
 
85  3100 toggle public static JSError make(String sourceName, int lineno, int charno,
86    DiagnosticType type, String... arguments) {
87  3100 return new JSError(sourceName, null, lineno, charno, type, null, arguments);
88    }
89   
90    /**
91    * Creates a JSError at a given source location
92    *
93    * @param sourceName The source file name
94    * @param lineno Line number with source file, or -1 if unknown
95    * @param charno Column number within line, or -1 for whole line.
96    * @param type The DiagnosticType
97    * @param arguments Arguments to be incorporated into the message
98    */
 
99  128 toggle public static JSError make(String sourceName, int lineno, int charno,
100    CheckLevel level, DiagnosticType type, String... arguments) {
101  128 return new JSError(
102    sourceName, null, lineno, charno, type, level, arguments);
103    }
104   
105    /**
106    * Creates a JSError from a file and Node position.
107    *
108    * @param sourceName The source file name
109    * @param n Determines the line and char position within the source file name
110    * @param type The DiagnosticType
111    * @param arguments Arguments to be incorporated into the message
112    */
 
113  4797 toggle public static JSError make(String sourceName, Node n,
114    DiagnosticType type, String... arguments) {
115  4797 return new JSError(sourceName, n, type, arguments);
116    }
117   
118    /**
119    * Creates a JSError from a file and Node position.
120    *
121    * @param n Determines the line and char position and source file name
122    * @param type The DiagnosticType
123    * @param arguments Arguments to be incorporated into the message
124    */
 
125  60 toggle public static JSError make(Node n, DiagnosticType type, String... arguments) {
126  60 return new JSError(n.getSourceFileName(), n, type, arguments);
127    }
128   
129    /**
130    * Creates a JSError from a file and Node position.
131    *
132    * @param sourceName The source file name
133    * @param n Determines the line and char position within the source file name
134    * @param type The DiagnosticType
135    * @param arguments Arguments to be incorporated into the message
136    */
 
137  925 toggle public static JSError make(String sourceName, Node n, CheckLevel level,
138    DiagnosticType type, String... arguments) {
139   
140  925 return new JSError(sourceName, n, n.getLineno(), n.getCharno(), type, level,
141    arguments);
142    }
143   
144    //
145    // JSError constructors
146    //
147   
148    /**
149    * Creates a JSError at a CheckLevel for a source file location.
150    * Private to avoid any entanglement with code outside of the compiler.
151    */
 
152  9052 toggle private JSError(
153    String sourceName, @Nullable Node node, int lineno, int charno,
154    DiagnosticType type, CheckLevel level, String... arguments) {
155  9052 this.type = type;
156  9052 this.node = node;
157  9052 this.description = type.format.format(arguments);
158  9052 this.lineNumber = lineno;
159  9052 this.charno = charno;
160  9052 this.sourceName = sourceName;
161  9052 this.defaultLevel = level == null ? type.level : level;
162  9052 this.level = level == null ? type.level : level;
163    }
164   
165    /**
166    * Creates a JSError for a source file location. Private to avoid
167    * any entanglement with code outside of the compiler.
168    */
 
169  4857 toggle private JSError(String sourceName, @Nullable Node node,
170    DiagnosticType type, String... arguments) {
171  4857 this(sourceName,
172    node,
173  4857 (node != null) ? node.getLineno() : -1,
174  4857 (node != null) ? node.getCharno() : -1,
175    type, null, arguments);
176    }
177   
 
178  28109 toggle public DiagnosticType getType() {
179  28109 return type;
180    }
181   
182    /**
183    * Format a message at the given level.
184    *
185    * @return the formatted message or {@code null}
186    */
 
187  598 toggle public String format(CheckLevel level, MessageFormatter formatter) {
188  598 switch (level) {
189  172 case ERROR:
190  172 return formatter.formatError(this);
191   
192  426 case WARNING:
193  426 return formatter.formatWarning(this);
194   
195  0 default:
196  0 return null;
197    }
198    }
199   
 
200  248 toggle @Override
201    public String toString() {
202    // TODO(user): remove custom toString.
203  248 return type.key + ". " + description + " at " +
204  248 (sourceName != null && sourceName.length() > 0 ?
205    sourceName : "(unknown source)") + " line " +
206  248 (lineNumber != -1 ? String.valueOf(lineNumber) : "(unknown line)") +
207  248 " : " + (charno != -1 ? String.valueOf(charno) : "(unknown column)");
208    }
209   
210    /**
211    * Get the character number.
212    */
 
213  11897 toggle public int getCharno() {
214  11897 return charno;
215    }
216   
217    /**
218    * Get the line number. One-based.
219    */
 
220  4 toggle public int getLineNumber() {
221  4 return lineNumber;
222    }
223   
224    /**
225    * @return the offset of the region the Error applies to, or -1 if the offset
226    * is unknown.
227    */
 
228  0 toggle public int getNodeSourceOffset() {
229  0 return node != null ? node.getSourceOffset() : -1;
230    }
231   
232    /**
233    * @return the length of the region the Error applies to, or 0 if the length
234    * is unknown.
235    */
 
236  0 toggle public int getNodeLength() {
237  0 return node != null ? node.getLength() : 0;
238    }
239   
240    /** The default level, before any of the WarningsGuards are applied. */
 
241  6170 toggle public CheckLevel getDefaultLevel() {
242  6170 return defaultLevel;
243    }
244   
 
245  0 toggle @Override
246    public boolean equals(Object o) {
247    // Generated by Intellij IDEA
248  0 if (this == o) {
249  0 return true;
250    }
251  0 if (o == null || getClass() != o.getClass()) {
252  0 return false;
253    }
254   
255  0 JSError jsError = (JSError) o;
256   
257  0 if (charno != jsError.charno) {
258  0 return false;
259    }
260  0 if (lineNumber != jsError.lineNumber) {
261  0 return false;
262    }
263  0 if (!description.equals(jsError.description)) {
264  0 return false;
265    }
266  0 if (defaultLevel != jsError.defaultLevel) {
267  0 return false;
268    }
269  0 if (sourceName != null ? !sourceName.equals(jsError.sourceName)
270    : jsError.sourceName != null) {
271  0 return false;
272    }
273  0 if (!type.equals(jsError.type)) {
274  0 return false;
275    }
276   
277  0 return true;
278    }
279   
 
280  66 toggle @Override
281    public int hashCode() {
282    // Generated by Intellij IDEA
283  66 int result = type.hashCode();
284  66 result = 31 * result + description.hashCode();
285  66 result = 31 * result + (sourceName != null ? sourceName.hashCode() : 0);
286  66 result = 31 * result + lineNumber;
287  66 result = 31 * result + defaultLevel.hashCode();
288  66 result = 31 * result + charno;
289  66 return result;
290    }
291    }